package com.pingan.haofang.searchcloud.web.monitor.controller;

import com.pingan.haofang.searchcloud.monitor.constants.MonitorIndicator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.pingan.haofang.framework.common.functional.lang.control.bean.BeanCopy;
import com.pingan.haofang.searchcloud.common.constants.WebConstants;
import com.pingan.haofang.searchcloud.common.dto.OperationResult;
import com.pingan.haofang.searchcloud.common.dto.PageDTO;
import com.pingan.haofang.searchcloud.monitor.constants.WarningType;
import com.pingan.haofang.searchcloud.monitor.dto.WarningConfigDTO;
import com.pingan.haofang.searchcloud.monitor.dto.WarningViewQueryDTO;
import com.pingan.haofang.searchcloud.monitor.dto.WarningViewQueryResult;
import com.pingan.haofang.searchcloud.monitor.service.DashboardWarningConfigService;
import com.pingan.haofang.searchcloud.web.monitor.form.ApiWarningConfigForm;
import com.pingan.haofang.searchcloud.web.monitor.form.HardwareWarningConfigForm;
import com.pingan.haofang.searchcloud.web.monitor.form.IndexWarningConfigForm;
import com.pingan.haofang.searchcloud.web.monitor.form.WarningViewQueryForm;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 搜索云预警配置
 *
 * @author EX-ZHOUYONG005
 * @date 2018/10/19
 * @since 1.0.0
 */
@RestController
@RequestMapping(WebConstants.WEB_PREFIX + "/dashboardWarningConfig")
@Api(tags = "dashboard.warningConfig", description = "搜索云预警配置")
public class DashboardWarningConfigController {

    @Autowired
    private DashboardWarningConfigService dashboardWarningConfigService;

    @ApiOperation("预警视图")
    @GetMapping(value = "/{warningType}")
    public PageDTO<WarningViewQueryResult>
    indexView(
            @ApiParam(value = "预警类型 1-导入预警,2-api预警,3-资源预警",
                    required = true) @PathVariable(value = "warningType", name = "warningType") int warningType,
            @ModelAttribute WarningViewQueryForm form) {

        WarningViewQueryDTO dto = BeanCopy.toFunction(WarningViewQueryDTO.class, BeanUtils::copyProperties).apply(form);
        dto.setWarningType(warningType);

        return dashboardWarningConfigService.queryWarningView(dto);
    }

    @ApiOperation("索引预警视图新建")
    @PostMapping(value = "/index")
    public OperationResult indexNew(@RequestBody IndexWarningConfigForm form) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(MonitorIndicator.INDEX.RECORD_COST, form.getRecordCost());
        // 这里存储为s为单位
        Double cost = form.getIndexCost() * 3600;
        jsonObject.put(MonitorIndicator.INDEX.INDEX_COST, cost.intValue());
        jsonObject.put(MonitorIndicator.INDEX.SUCCESS_RATE, form.getSuccessRate());

        WarningConfigDTO dto = BeanCopy.toFunction(WarningConfigDTO.class, BeanUtils::copyProperties).apply(form);

        dto.setWarningType(WarningType.INDEX.getValue());
        dto.setWarningConfig(jsonObject.toJSONString());
        return dashboardWarningConfigService.warningConfigNew(dto);
    }

    @ApiOperation("API预警视图新建")
    @PostMapping(value = "/api")
    public OperationResult apiNew(@RequestBody ApiWarningConfigForm form) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(MonitorIndicator.API.RECORD_COST, form.getRecordCost());
        jsonObject.put(MonitorIndicator.API.WARNING_NUM, form.getWarningNum());

        WarningConfigDTO dto = BeanCopy.toFunction(WarningConfigDTO.class, BeanUtils::copyProperties).apply(form);

        dto.setWarningType(WarningType.API.getValue());
        dto.setWarningConfig(jsonObject.toJSONString());
        return dashboardWarningConfigService.warningConfigNew(dto);
    }

    @ApiOperation("硬件视图新建")
    @PostMapping(value = "/hardware")
    public OperationResult hardwareNew(@RequestBody HardwareWarningConfigForm form) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(MonitorIndicator.HARDWARE.DISK_OCCUPANCY_RATE, form.getDiskOccupancyRate());
        jsonObject.put(MonitorIndicator.HARDWARE.SOLR_HEALTH_RATE, form.getSolrHealthRate());

        WarningConfigDTO dto = BeanCopy.toFunction(WarningConfigDTO.class, BeanUtils::copyProperties).apply(form);

        dto.setWarningType(WarningType.RESOURCE.getValue());
        dto.setWarningConfig(jsonObject.toJSONString());
        return dashboardWarningConfigService.warningConfigNew(dto);
    }

    @ApiOperation("预警配置删除")
    @DeleteMapping(value = "/{id}")
    public OperationResult
    delete(@ApiParam(value = "主键ID", required = true) @PathVariable(value = "id", name = "id") Long id) {
        return dashboardWarningConfigService.deleteWarningConfig(id);
    }

}
